package components.product
{
	import cultu.supplier.message.Association;
	import cultu.supplier.message.Product;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import mx.core.FlexGlobals;
	import mx.core.IVisualElement;
	import mx.events.ResizeEvent;
	import mx.managers.SystemManager;
	
	import spark.components.Application;
	import spark.core.SpriteVisualElement;
	
	import vo.ProductVO;
	import vo.community.CommunityVO;
	
	import wizeoo.models.WIZGlobalParameters;
	
	public class AssociationGroup extends SpriteVisualElement
	{
		
		private const CHILDREN_WIDTH:int = 140;
		
		private const CHILDREN_HEIGHT:int = 220;
		
		private const CHILDREN_GAP:int = 15;
		
		private var _data:Association;
		
		private var _created:Boolean = false;
		
		private var _childrens:Vector.<ProductSprite>;
		
		private var _title:TextField;
		
		private const TITLE_TEXTFORMAT:TextFormat = new TextFormat("Arial",20,0x555555,false);
		
		public function AssociationGroup()
		{
			super();
			
			WIZGlobalParameters.root.addEventListener(ResizeEvent.RESIZE,onResize,false,0,true);
		}
		
		public function set data(pValue:Association):void
		{
			if(pValue != _data && pValue != null)
			{
				_data = pValue;
				
				if(!_created)
				{
					createChildren();
				}
				else
				{
					updateDisplay();
				}
			}
		}
		
		protected function createChildren():void
		{
			_created = true;
			
			_title = new TextField();
			var comm:CommunityVO = new CommunityVO(_data.community);
			_title.text = comm.label;
			_title.setTextFormat(TITLE_TEXTFORMAT);
			_title.x = 5;
			_title.y = 5;
			_title.width = this.width;
			addChild(_title);
			
			var pX:int = 5;
			var pY:int = 35;
			
			_childrens = new Vector.<ProductSprite>;
			
			var draw:Boolean = true;
			var i:int = 0;
			
			while(draw && i < _data.products.length)
			{
				var p:Product = _data.products.getItemAt(i) as Product;
				var pVO:ProductVO = new ProductVO(p);
				
				var productSprite:ProductSprite = new ProductSprite();
				
				addChild(productSprite);
				
				productSprite.width = CHILDREN_WIDTH;
				productSprite.height = CHILDREN_HEIGHT;
				productSprite.x = pX;
				productSprite.y = pY;
				productSprite.data = pVO;
				
				_childrens.push(productSprite);
				
				pX += CHILDREN_WIDTH + CHILDREN_GAP;
				
				i++;
				
				var point:Point = productSprite.localToGlobal(new Point(pX,pY));
				if(point.x > WIZGlobalParameters.root.width)
				{
					draw = false;
				}
			}
		}
		
		public function onResize(pEvent:ResizeEvent):void
		{
			if(_childrens.length > 0 && _data.products.length > 0)
			{
				
				var lastChild:ProductSprite = _childrens[_childrens.length-1];
				
				if(lastChild.x + CHILDREN_WIDTH > WIZGlobalParameters.root.width)
				{
					//removeChildrenSize();
				}
				else
				{
					addChildrenSize();
				}
				
			}
		}
		
		private function removeChildrenSize():void
		{
			var applicationWidth:int = WIZGlobalParameters.root.width;
			
			var lastChild:ProductSprite = _childrens[_childrens.length-1];
			
			var removed:int = 0;
			
			var point:Point = lastChild.localToGlobal(new Point(lastChild.x,lastChild.y));
			
			while(lastChild.x + lastChild.width > applicationWidth)
			{
				removeChildAt(getChildIndex(lastChild));
				removed += 1;
			}
			
			_childrens = _childrens.slice(0,_childrens.length - removed);
		}
		
		private function addChildrenSize():void
		{
			var lastChild:ProductSprite = _childrens[_childrens.length-1];
			var pX:int = lastChild.x + CHILDREN_WIDTH + CHILDREN_GAP;
			var pY:int = 35;
			
			var point:Point = localToGlobal(new Point(pX+CHILDREN_WIDTH,pY));
			var draw:Boolean = point.x < WIZGlobalParameters.root.width;
			var i:int = _childrens.length;
			
			while(draw && i < _data.products.length)
			{
				var p:Product = _data.products.getItemAt(i) as Product;
				var pVO:ProductVO = new ProductVO(p);
				
				var productSprite:ProductSprite = new ProductSprite();
				
				addChild(productSprite);
				
				productSprite.width = CHILDREN_WIDTH;
				productSprite.height = CHILDREN_HEIGHT;
				productSprite.x = pX;
				productSprite.y = pY;
				productSprite.data = pVO;
				
				_childrens.push(productSprite);
				
				pX += CHILDREN_WIDTH + CHILDREN_GAP;
				
				i++;
				
				point = productSprite.localToGlobal(new Point(pX,pY));
				
				if(point.x > WIZGlobalParameters.root.width)
				{
					draw = false;
				}
			}
		}
		
		public function updateDisplay():void
		{
			
		}
	}
}